<template>
  <el-menu
    default-active="1-4-1"
    class="el-menu-vertical-demo"
    @open="handleOpen"
    @close="handleClose"
    :collapse="isCollapse"
    background-color="#545c64"
    text-color="#fff"
    active-text-color="#409EFF"
    :collapse-transition="false"
  >

    <h3>{{isCollapse?"后台":"通用后台管理系统"}}</h3>
    <el-menu-item @click="clickMeau(m)" v-for="m in noChildren" :key="m.name" :index="m.name">
      <i :class="`el-icon-${m.icon}`"></i>
      <span slot="title">{{ m.label }}</span>
    </el-menu-item>
    <el-submenu v-for="m in hasChildren" :key="m.name" :index="m.name">
      <template slot="title">
        <i :class="`el-icon-${m.icon}`"></i>
        <span slot="title">{{ m.label }}</span>
      </template>
      <el-menu-item-group v-for="sub in m.children" :key="sub.name">
        <el-menu-item @click="clickMeau(sub)" :index="sub.name">{{ sub.label }}</el-menu-item>
      </el-menu-item-group>
    </el-submenu>
  </el-menu>
</template>

<script>
import {mapState} from 'vuex'
export default {
  name: "Navigation",
  data(){
    return {
      menu:[]
    }
  },
  mounted(){
    this.menu = JSON.parse(localStorage.getItem('menu'))
  },
  computed:{
    ...mapState('navigation',['isCollapse']),

    // 将左侧导航的数据分为有子节点，和没有子节点两类，进行遍历，生成对应结构
    noChildren() {
      return this.menu.filter((v) => v.children == null);
    },
    hasChildren() {
      return this.menu.filter((v) => v.children != null);
    },
  },
  methods: {
    handleOpen(key, keyPath) {
      // console.log(key, keyPath);
    },
    handleClose(key, keyPath) {
      // console.log(key, keyPath);
    },
    // 导航单机事件回调
    clickMeau(meau){
      if(this.$route.fullPath != meau.path){
        this.$router.push(meau.path)
        // 修改Header组件中的tangList
        this.$store.commit('header/GETTAGLIST',meau)
      }
    }
  }
};
</script>

<style lang='less' scoped>
.el-menu-vertical-demo {
  // 表示高度占满屏幕
  height: 100vh;
  border-right: 1px solid #555;
  h3{
    line-height: 50px;
    color: white;
    font-weight: bold;
    text-align: center;
    font-size: 20px;
  }

  .el-submenu .el-menu-item{
    min-width: 0;
  }
}
</style>